package com.jdbc;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionTest {

    //方式一:
    @Test
    public void testConnection1() throws SQLException {

        Driver driver = new com.mysql.jdbc.Driver();


        String url = "jdbc:mysql://192.168.189.143:33061/hibernate";
        //将用户名和密码封装在Properties中。
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "123456");


        Connection connection = driver.connect(url, info);

        System.out.println(connection);
    }


    //方式二: 对方式一的迭代
    @Test
    public void test2() throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {

        //1.获取Driver实现对象，使用反射来获取。
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        String url = "jdbc:mysql://192.168.189.143:33061/hibernate";
        //将用户名和密码封装在Properties中。
        Properties info = new Properties();
        info.setProperty("user", "root");
        info.setProperty("password", "123456");


        Connection connection = driver.connect(url, info);

        System.out.println(connection);

    }

    //方式三: 使用DriverManager获取连接
    @Test
    public void test3() throws ClassNotFoundException, SQLException, IllegalAccessException, InstantiationException {

        //1.获取Driver实现对象，使用反射来获取。
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) aClass.newInstance();

        //2.提供另外三个连接的基本信息
        String url = "jdbc:mysql://192.168.189.143:33061/hibernate";
        String user = "root";
        String password = "123456";


        //注册驱动
        DriverManager.registerDriver(driver);


        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }


    //方法四:优化方式三
    @Test
    public void test4() throws ClassNotFoundException, SQLException, IllegalAccessException, InstantiationException {

        //1.提供另外三个连接的基本信息
        String url = "jdbc:mysql://192.168.189.143:33061/hibernate";
        String user = "root";
        String password = "123456";


        //2.获取Driver实现对象，使用反射来获取。
        Class.forName("com.mysql.jdbc.Driver");
        //相较于方式三，可以省略如下的操作
//        Driver driver = (Driver) aClass.newInstance();
//
//
//        //注册驱动
//        DriverManager.registerDriver(driver);
        //为什么可以省略上述的操作呢，因为在Driver实现类中提前声明了如下操作
//        static {
//            try {
//                DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//            } catch (SQLException var1) {
//                throw new RuntimeException("Can't register driver!");
//            }
//        }


        //获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println(connection);
    }


    /**
     * 方式五: 将配置信息放到配置文件，不那么Low
     */
    @Test
    public void test5() throws IOException, ClassNotFoundException, SQLException {

        //1.读取配置文件中的4个基本信息
        InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("D:\\xuehujavaSE01\\jdbc.properties");


        Properties properties = new Properties();

        properties.load(resourceAsStream);

        String user = properties.getProperty("user");

        String password = properties.getProperty("password");

        String url = properties.getProperty("url");

        String driverClass = properties.getProperty("driverClass");

        Class.forName(driverClass);


        Connection connection = DriverManager.getConnection(url, user, password);

        System.out.println(connection);


    }


}
